#include "FKLiveButton.h"

#include "FKLiveButtonReferent.h"
#include "FKEventNames.h"

#include "FKLogger.h"
#include "FKSDM.h"

FK_EVENTS(FKLiveButton){
actions.insert(FKEventName::a_buttonClick,&FKLiveButton::A_click);
props.append(FKEventName::c_color);
props.append(FKEventName::c_score);
props.append(FKEventName::c_position);
}

FKLiveButton::FKLiveButton(FKRoom* room,qint32 objectID):FKObject(room,objectID),
        _color(Qt::gray),_score(0),_lastScore(0){
    FK_CBEGIN
    _view=0;
//    if(objectID==-1){
//        servant=new Servant;
//    }else{
//        servant=0;
//    }
//    customValues=0;
    FK_CONSTRUCTOR
    FK_CEND
}

void FKLiveButton::customInitialization(){
    _view=new FKLiveButtonReferent(FKSDM::quad,getID(),this);
    _view->show();
//    _lastScore=0;
//    C_score(_score);
//    C_offset(_xOffset);
    C_color(_color);
}

void FKLiveButton::customDeinitialization(){
    _view->deleteLater();
    _view=0;
}

FKLiveButton::~FKLiveButton(){
    FK_DBEGIN
    FK_DESTRUCTOR
    //if(servant)delete servant;
    FK_DEND
}

void FKLiveButton::move(float xOffset){
    _xOffset=xOffset;
    updateProp(FKEventName::c_position);
}

void FKLiveButton::A_click(FKEvent* event){
    Q_UNUSED(event)
    emit clicked(this);
}

//QColor FKLiveButton::color() const{
//    return _color;
//}

void FKLiveButton::setColor(const QColor &c){
    servant->color=c;
    updateProp(FKEventName::c_color);
}

void FKLiveButton::C_color(const QColor c){
    _color=c;
    if(_view)_view->setColor(_color);
}

void FKLiveButton::setScore(qint32 score){
    _score=score;
    updateProp(FKEventName::c_score);
}

void FKLiveButton::addScore(qint32 score){
    setScore(_score+score);
}

void FKLiveButton::C_score(const qint32 score){
    qint32 delta=score-_lastScore;
    _score=score;
    _lastScore=score;
    const float speed=0.5f;
    if(_view)_view->enqueScale(delta*speed);
}

void FKLiveButton::C_offset(const float offset){
    _xOffset=offset;
    if(_view)_view->setPos(QVector3D(_xOffset,0,-1));
}
